package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Commodity;
import com.ruoyi.system.domain.vo.CollectVo;
import com.ruoyi.system.service.ICommodityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Collect;
import com.ruoyi.system.service.ICollectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收藏Controller
 * 
 * @author ruoyi
 * @date 2024-03-10
 */
@RestController
@RequestMapping("/system/collect")
public class CollectController extends BaseController
{
    @Autowired
    private ICollectService collectService;
    @Autowired
    private ICommodityService commodityService;
    /**
     * 查询本人收藏列表
     */
    //@PreAuthorize("@ss.hasPermi('system:collect:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(Collect collect)
//    {
//        startPage();
//        List<Collect> list = collectService.selectCollectList(collect);
//        return getDataTable(list);
//    }
    @GetMapping("/myCollectList")
    public TableDataInfo myCollectList()
    {
        startPage();
        Collect collect=new Collect();
        collect.setPublisherId(getUserId());
        List<CollectVo> list = collectService.selectMyCollectList(collect);
        return getDataTable(list);
    }
    /**
     * 导出收藏列表
     */
    //@PreAuthorize("@ss.hasPermi('system:collect:export')")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Collect collect)
    {
        List<Collect> list = collectService.selectCollectList(collect);
        ExcelUtil<Collect> util = new ExcelUtil<Collect>(Collect.class);
        util.exportExcel(response, list, "收藏数据");
    }

    /**
     * 获取收藏详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Collect query=new Collect();
        query.setCommodityId(id);
        query.setPublisherId(getUserId());
        List<Collect> collects = collectService.selectCollectList(query);
        return success(!collects.isEmpty());
    }

    /**
     * 新增收藏
     */
    //@PreAuthorize("@ss.hasPermi('system:collect:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Collect collect)
    {
        Collect collect1 = new Collect();
        collect1.setPublisherId(getUserId());
        collect1.setCommodityId(collect.getCommodityId());
        if (commodityService.selectCommodityById(collect.getCommodityId())==null) {
            return error("commodityId不存在！");
        }else if (!collectService.selectCollectList(collect1).isEmpty()){
            return error("当前商品已收藏，您不能重复收藏！");
        }
        collect.setPublisherId(getUserId());
        return toAjax(collectService.insertCollect(collect));
    }

    /**
     * 修改收藏
     */
    //@PreAuthorize("@ss.hasPermi('system:collect:edit')")
//    @Log(title = "收藏", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody Collect collect)
//    {
//        if (collectService.selectCollectById(collect.getId())==null) {
//            return error("当前id没有操作权限！");
//        }
//        return toAjax(collectService.updateCollect(collect));
//    }

    /**
     * 删除收藏
     */
    //@PreAuthorize("@ss.hasPermi('system:collect:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> delIds=new ArrayList();
        for(Long id:ids){
            Collect collect = new Collect();
            collect.setCommodityId(id);
            collect.setPublisherId(getUserId());
            List<Collect> collects = collectService.selectCollectList(collect);
            if (collects.size()==0){
                return error("包含无效的id值！");
            }
            for (Collect collect1 : collects) {
                delIds.add(collect1.getId());
            }
        }
        if (delIds.size()==0){
            return toAjax(0);
        }
        return toAjax(collectService.deleteCollectByIds(delIds.toArray(new Long[delIds.size()])));
    }
}
